package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import com.blankj.utilcode.util.SizeUtils;
import com.wswy.wzcx.R;
import java.util.LinkedList;
import java.util.List;

public class CircleStepLineView extends View {

  public CircleStepLineView(Context context) {
    this(context,null);
  }

  public CircleStepLineView(Context context,
      @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  public CircleStepLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


    radius = SizeUtils.dp2px(6);
    strokeWidth = SizeUtils.dp2px(2f);

    lineWidth = SizeUtils.dp2px(1.5f);

    circleColor = ContextCompat.getColor(context, R.color.textColorPrimary_20);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(strokeWidth);
    paint.setColor(circleColor);

    pathEffect = new DashPathEffect(new float[]{SizeUtils.dp2px(2),SizeUtils.dp2px(2)},0);
  }

  private List<RectF> circlesList = new LinkedList<>();

  private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

  private Path pathLine = new Path();
  private PathEffect pathEffect =null;

  private int strokeWidth,radius;

  private int circleColor;

  private int lineWidth;

  public void attachAnchorViews(final View... views){
    circlesList.clear();


    final ViewTreeObserver viewTreeObserver = ((View) getParent()).getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {

            for (View view : views) {

              int top = view.getTop();
              int bottom = view.getBottom();
              float ct = top+((bottom-top)/2f-radius);
              RectF rect = new RectF(getPaddingLeft(),ct,radius*2f,ct+radius*2f);

              circlesList.add(rect);
            }

            invalidate();

            ((View) getParent()).getViewTreeObserver().removeOnGlobalLayoutListener(this);
          }
        });

  }


  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(!circlesList.isEmpty()){

      RectF beforeRect = null;

      for (RectF rect : circlesList) {
        paint.setPathEffect(null);
        paint.setStrokeWidth(strokeWidth);
        canvas.drawCircle(rect.centerX(),rect.centerY(),radius-strokeWidth,paint);

        if(beforeRect != null){
          paint.setStrokeWidth(lineWidth);
          paint.setPathEffect(pathEffect);

          pathLine.reset();

          pathLine.moveTo(beforeRect.centerX(),beforeRect.bottom);
          pathLine.lineTo(rect.centerX(),rect.top);

          canvas.drawPath(pathLine,paint);

          //canvas.drawLine(beforeRect.centerX(),beforeRect.bottom-strokeWidth,rect.centerX(),rect.top+strokeWidth,paint);

        }
        beforeRect = rect;
      }

    }


  }
}
